﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.Model.Mail;
using Jzy.BLL.Mail;
using Jzy.Model;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Module;
using Frame.Lib.Enums;


public partial class Mail_Folder_List : PageList
{
    private MailFolderType FolderType
    {
        get
        {
            if (Request["FolderType"] == null || Request["FolderType"] == "")
            {
                return MailFolderType.内部邮件;
            }
            else
            {
                return (MailFolderType)int.Parse(Request["FolderType"]);
            }
        }
    }

    private MailFolderBO folder;
    protected void Page_Load(object sender, EventArgs e)
    {
        folder = new MailFolderBO(AppSession.UserID, FolderType);

        if (!IsPostBack)
        {
            BindView();
        }
        AddAttr();
    }

    private void AddAttr()
    {
        JSUtil.ShowForm(lnkAdd, "增加文件夹", null, GlobalVar.WebCurrPath + "Edit.aspx?FolderType=" + (int)FolderType, 400, 154);
    }

    private void BindView()
    {
        GVUtil.InitGV(gvList);
        gvList.EnableViewState = true;
        DataTable dt = folder.GetCountList();
        gvList.DataKeyNames = new string[]{"ID"};
        gvList.DataSource = dt.DefaultView;
        gvList.DataBind();
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindView();
        ModuleBO module = new ModuleBO();
        object receiveBoxModuleID = module.GetIDByCode(ModuleCode.ReceiveBox);
        if (receiveBoxModuleID != null)
        {
            ModuleInfo info = module.GetInfo(receiveBoxModuleID);
            string destPath = null;
            if (FolderType == MailFolderType.内部邮件)
            {
                destPath = GlobalVar.WebAppPath + "Mail/Inner/";
            }
            else
            {
                destPath = GlobalVar.WebAppPath + "Mail/Web/";
            }
            folder.AddCustMailFolder(info.UpperID,true,destPath);
        }

    }
    protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink lnkName = (HyperLink)e.Row.FindControl("lnkName");
            if (FolderType == MailFolderType.内部邮件)
            {
                lnkName.NavigateUrl = "../Inner/List.aspx?FolderID=" + gvList.DataKeys[e.Row.RowIndex]["ID"];
            }
            else
            {
                lnkName.NavigateUrl = "../Web/List.aspx?FolderID=" + gvList.DataKeys[e.Row.RowIndex]["ID"];
            }

            LinkButton btnDelete = (LinkButton)e.Row.FindControl("btnDelete");
            btnDelete.Attributes.Add("onclick", "return confirm('确认删除目录?')");

            LinkButton btnClear = (LinkButton)e.Row.FindControl("btnClear");
            btnClear.Attributes.Add("onclick", "return confirm('确认清除所有邮件?')");

            HyperLink lnkEdit = (HyperLink)e.Row.FindControl("lnkEdit");
            JSUtil.ShowForm(lnkEdit, "修改目录", null, GlobalVar.WebCurrPath + "Edit.aspx?FolderType=" + (int)FolderType + "&ID=" + gvList.DataKeys[e.Row.RowIndex]["ID"], 400, 154);
        }
    }
    protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        folder.Delete((int)gvList.DataKeys[e.RowIndex]["ID"]);
        btnRefresh_Click(sender, e);
    }
    protected void gvList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        folder.Clear(gvList.DataKeys[e.NewSelectedIndex]["ID"]);
        BindView();
    }
}
